package com.example.communitypro.service.impl;

import com.example.communitypro.mapper.CommunityInformationMapper;
import com.example.communitypro.mapper.UserCollectionMapper;
import com.example.communitypro.pojo.CommunityInformation;
import com.example.communitypro.service.CommunityInformationService;
import com.example.communitypro.util.result.Result;
import com.example.communitypro.util.result.ResultCodeEnum;
import com.example.communitypro.util.result.ResultCodeEnumOwn;
import com.example.communitypro.util.result.ResultOwn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service

public class CommunityInformationServiceImpl implements CommunityInformationService {
    @Autowired(required = false)
    private CommunityInformationMapper communityInformationMapper;


    @Override
    public Result addCommunityInformation(CommunityInformation record){
        try {
            record.setCommunityMember(0);
            record.setCommunityLikeNumber(0);
            record.setCommunityCollectionNumber(0);
            record.setCommunityPostNumber(0);
            if(communityInformationMapper.insertSelective(record)>0)
            {
                return Result.ok().message("新增社区信息成功");
            }
            else{
                return Result.error().message("新增社区信息失败");
            }
        }catch (Exception e){
            return Result.error().message("新增社区信息失败");
        }
    }


    @Override
    public Result dropCommunityInformation(int communityNo){
        try {
            if(communityInformationMapper.deleteByPrimaryKey(communityNo)>0)
            {
                return Result.ok().message("删除社区信息成功");
            }
            else{
                return Result.error().message("删除社区信息失败");
            }
        }catch (Exception e){
            return Result.error().message("删除社区信息失败");
        }
    }

    @Override
    public Result modifyCommunityInformation(CommunityInformation record){
        try {
            if(communityInformationMapper.updateByPrimaryKeySelective(record)>0)
            {
                return Result.ok().message("修改成功");
            }
            else{
                return Result.error().message("修改失败：未找到对应记录");
            }
        }catch (DuplicateKeyException e){
            System.err.println(e);
            return Result.error().message("修改失败：已经存在对应记录");
        }
        catch (Exception e){
            System.err.println(e);
            return Result.error().message("修改失败："+e.toString());
        }
    }

    @Override
    public Result queryCommunityInformation(int communityNo){
        try {
            CommunityInformation communityInformation=communityInformationMapper.selectByPrimaryKey(communityNo);

            List<CommunityInformation> res = new ArrayList<>();
            res.add(communityInformation);

            if(communityInformation!=null)
            {
                return Result.ok().message("查找社区信息成功").data("items",res);
            }
            else{
                return Result.error().message("查找社区信息失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.error().message("查找社区信息失败");
        }
    }


    @Override
    public Result findCommunityInformationAll(){
        try {
            List<CommunityInformation> communityInformationAll = communityInformationMapper.findCommunityInformationAll();
            if(communityInformationAll!=null)
            {
                return Result.ok().message("查找社区信息成功").data("items",communityInformationAll).count(communityInformationAll.size());
            }
            else{
                return Result.error().message("查找社区信息失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.error().message("查找社区信息失败");
        }
    }

    @Override
    public Result findCommunityInformationDynamic(int communityNo,String communityName) {
        try {
            List<CommunityInformation> communityInformationAll = communityInformationMapper.findCommunityInformationDynamic(communityNo,communityName);
            if(communityInformationAll!=null)
            {
                return Result.ok().message("查找社区信息成功").data("items",communityInformationAll).count(communityInformationAll.size());
            }
            else{
                return Result.error().message("查找社区信息失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.error().message("查找社区信息失败");
        }
    }
}
